﻿namespace HutongGames.PlayMakerEditor
{
    using HutongGames.PlayMaker;
    using System;
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    internal class FsmInspector
    {
        private static bool chooseWatermark;
        private static List<FsmVariable> fsmVariables = new List<FsmVariable>();
        private static bool isInitialized;
        private static Vector2 scrollViewPosition;

        private static void BuildFsmVariableList(Fsm fsm)
        {
            fsmVariables = FsmVariable.GetFsmVariableList(fsm.Variables, fsm.Owner);
            List<FsmVariable> list = new List<FsmVariable>();
            foreach (FsmVariable variable in fsmVariables)
            {
                if (!variable.ShowInInspector)
                {
                    list.Add(variable);
                }
            }
            foreach (FsmVariable variable2 in list)
            {
                fsmVariables.Remove(variable2);
            }
            fsmVariables.Sort();
        }

        public static void Init()
        {
            if (FsmEditor.SelectedFsm != null)
            {
                BuildFsmVariableList(FsmEditor.SelectedFsm);
            }
        }

        public static void OnGUI()
        {
            GUI.changed = false;
            Fsm selectedFsm = FsmEditor.SelectedFsm;
            if (selectedFsm == null)
            {
                GUILayout.FlexibleSpace();
            }
            else
            {
                if (!isInitialized)
                {
                    isInitialized = true;
                    Init();
                }
                scrollViewPosition = GUILayout.BeginScrollView(scrollViewPosition, new GUILayoutOption[0]);
                selectedFsm.Name = EditorGUILayout.TextField(selectedFsm.Name, new GUILayoutOption[0]);
                selectedFsm.Description = FsmEditorGUILayout.TextAreaWithHint(selectedFsm.Description, "Description...", new GUILayoutOption[] { GUILayout.MinHeight(80f) });
                EditorGUILayout.BeginHorizontal(new GUILayoutOption[0]);
                selectedFsm.DocUrl = FsmEditorGUILayout.TextFieldWithHint(selectedFsm.DocUrl, "Documentation Url...", new GUILayoutOption[0]);
                bool enabled = GUI.enabled;
                if (string.IsNullOrEmpty(selectedFsm.DocUrl))
                {
                    GUI.enabled = false;
                }
                if (FsmEditorGUILayout.HelpButton("Online Help"))
                {
                    Application.OpenURL(selectedFsm.DocUrl);
                }
                EditorGUILayout.EndHorizontal();
                GUI.enabled = enabled;
                selectedFsm.RestartOnEnable = GUILayout.Toggle(selectedFsm.RestartOnEnable, new GUIContent("Reset On Disable", "Should the FSM reset or keep its current state on Enable/Disable. Uncheck this if you want to pause an FSM by enabling/disabling the PlayMakerFSM component."), new GUILayoutOption[0]);
                selectedFsm.ShowStateLabel = GUILayout.Toggle(selectedFsm.ShowStateLabel, new GUIContent("Show State Label", "Show active state label in game view.\nNOTE: Requires PlayMakerGUI in scene"), new GUILayoutOption[0]);
                selectedFsm.EnableDebugFlow = GUILayout.Toggle(selectedFsm.EnableDebugFlow, new GUIContent("Enable Debug Flow", "Enable caching of variables and other state info while the game is running. NOTE: Disabling this can improve performance in the editor (it is always disabled in standalone builds)."), new GUILayoutOption[0]);
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label(new GUIContent("Controls", "Events and Variables exposed in the Inspector."), EditorStyles.boldLabel, new GUILayoutOption[0]);
                if ((selectedFsm.ExposedEvents.Count + fsmVariables.Count) == 0)
                {
                    FsmEditorGUILayout.DisabledLabel("[None]");
                }
                else
                {
                    foreach (FsmVariable variable in fsmVariables)
                    {
                        if (variable.ShowInInspector)
                        {
                            variable.DoValueGUI(new GUIContent(variable.Name, variable.Name + (!string.IsNullOrEmpty(variable.Tooltip) ? (":\n" + variable.Tooltip) : "")), false);
                        }
                    }
                    foreach (FsmEvent event2 in selectedFsm.ExposedEvents)
                    {
                        if (GUILayout.Button(event2.Name, new GUILayoutOption[0]))
                        {
                            selectedFsm.Event(event2);
                        }
                    }
                    if (GUI.changed)
                    {
                        FsmEditor.RepaintAll();
                    }
                }
                if (FsmEditorSettings.ShowHints)
                {
                    GUILayout.Box("Expose Events and Variables as controls in the Events and Variables Tabs by checking the Inspector option.\nWhile the game is running, you can use the controls to test and tweak the FSM.", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.Label(new GUIContent("Network Sync", "Variables set to NetworkSync in the Variables tab."), EditorStyles.boldLabel, new GUILayoutOption[0]);
                int num = 0;
                foreach (FsmBool @bool in selectedFsm.Variables.BoolVariables)
                {
                    if (@bool.NetworkSync)
                    {
                        GUILayout.Label(@bool.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmFloat num2 in selectedFsm.Variables.FloatVariables)
                {
                    if (num2.NetworkSync)
                    {
                        GUILayout.Label(num2.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmInt num3 in selectedFsm.Variables.IntVariables)
                {
                    if (num3.NetworkSync)
                    {
                        GUILayout.Label(num3.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmQuaternion quaternion in selectedFsm.Variables.QuaternionVariables)
                {
                    if (quaternion.NetworkSync)
                    {
                        GUILayout.Label(quaternion.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                foreach (FsmVector3 vector in selectedFsm.Variables.Vector3Variables)
                {
                    if (vector.NetworkSync)
                    {
                        GUILayout.Label(vector.Name, new GUILayoutOption[0]);
                        num++;
                    }
                }
                if (num == 0)
                {
                    FsmEditorGUILayout.DisabledLabel("[none]");
                }
                if (FsmEditorSettings.ShowHints)
                {
                    GUILayout.Box("Check NetworkSync in the Variables Tab to automatically sync a variable across the network. NOTE: The GameObject requires a Network View that Observes the PlayMakerFSM (drag this PlayMakerFSM into the Observed field).", FsmEditorStyles.HintBox, new GUILayoutOption[0]);
                }
                FsmEditorGUILayout.LightDivider(new GUILayoutOption[0]);
                GUILayout.EndScrollView();
                EventType type = Event.current.type;
                if (GUI.changed)
                {
                    FsmEditor.SetFsmDirty(false, false);
                }
            }
        }

        public static void Update()
        {
            if (chooseWatermark)
            {
                chooseWatermark = false;
                EditorCommands.ChooseWatermark();
            }
        }
    }
}

